// store.index.js
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import category from './modules/category'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    user: user,
    category
  },
  plugins: [createPersistedState({
    key: 'cms-auth',
    paths: ['user.token', 'user.user'], // 指向模块中的路径
    reducer: (state) => {
      // 添加状态验证
      if (!state.user) {
        console.warn('Persisting empty user state')
        return {
          user: {
            token: '',
            user: null
          }
        }
      }
      return { user: state.user }
    }
  })]
})